package org.jarraiders.musiclibrary.client.view;

import java.util.ArrayList;
import java.util.List;

import org.jarraiders.musiclibrary.client.presenter.AlbumsPresenter;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class AlbumsView extends Composite implements AlbumsPresenter.Display {
	private final Button addButton;
	private final Button deleteButton;
	private FlexTable albumsTable;
	private final FlexTable contentTable;
	private Button searchButton;
	private TextBox searchTextBox;
	private int indexOfPopup;

	public AlbumsView() {
		DecoratorPanel contentTableDecorator = new DecoratorPanel();
		initWidget(contentTableDecorator);
		contentTableDecorator.setWidth("100%");
		contentTableDecorator.setWidth("18em");

		contentTable = new FlexTable();
		contentTable.setWidth("100%");
		contentTable.getCellFormatter().addStyleName(0, 0, "library-ListContainer");
		contentTable.getCellFormatter().setWidth(0, 0, "100%");
		contentTable.getFlexCellFormatter().setVerticalAlignment(0, 0, DockPanel.ALIGN_TOP);

		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setBorderWidth(0);
		hPanel.setSpacing(0);
		hPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
		addButton = new Button("Add");
		hPanel.add(addButton);
		deleteButton = new Button("Delete");
		hPanel.add(deleteButton);
		contentTable.getCellFormatter().addStyleName(0, 0, "library-ListMenu");
		contentTable.setWidget(0, 0, hPanel);

		HorizontalPanel searchPanel = new HorizontalPanel();
		searchButton = new Button("Search");
		searchTextBox = new TextBox();
		searchPanel.add(searchTextBox);
		searchPanel.add(searchButton);
		contentTable.setWidget(1, 0, searchPanel);

		albumsTable = new FlexTable();
		albumsTable.setCellSpacing(0);
		albumsTable.setCellPadding(0);
		albumsTable.setWidth("100%");
		albumsTable.addStyleName("library-ListContents");
		albumsTable.getColumnFormatter().setWidth(0, "15px");
		contentTable.setWidget(2, 0, albumsTable);

		contentTableDecorator.add(contentTable);
	}

	@Override
	public HasClickHandlers getAddButton() {
		return addButton;
	}

	@Override
	public HasClickHandlers getDeleteButton() {

		return deleteButton;
	}

	@Override
	public HasClickHandlers getList() {

		return albumsTable;
	}

	@Override
	public void setData(final List<String> data) {
		albumsTable.removeAllRows();
		indexOfPopup = -1;

		for (int i = 0; i < data.size(); ++i) {

			final AlbumsIcon albumIcon = new AlbumsIcon();

			albumIcon.setText(data.get(i));
			final int pushedIndex = i;
			albumIcon.addMouseOverHandler(new MouseOverHandler() {

				@Override
				public void onMouseOver(MouseOverEvent event) {
					setPopup(pushedIndex, albumIcon);
				}
			});

			albumsTable.setWidget(i / 6, i % 6, albumIcon);

		}
	}

	@Override
	public int getClickedRow(ClickEvent event) {
		int selectedRow = -1;
		HTMLTable.Cell cell = albumsTable.getCellForEvent(event);

		if (cell != null) {
			selectedRow = cell.getCellIndex();
		}

		return selectedRow;
	}

	@Override
	public List<Integer> getSelectedRows() {
		List<Integer> selectedRows = new ArrayList<Integer>();

		for (int i = 0; i < albumsTable.getRowCount(); ++i) {
			CheckBox checkBox = (CheckBox) albumsTable.getWidget(i, 0);
			if (checkBox.getValue()) {
				selectedRows.add(i);
			}
		}
		return selectedRows;
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public HasClickHandlers getSearchButton() {

		return searchButton;
	}

	@Override
	public String getSearchText() {

		return searchTextBox.getText();
	}

	private void setPopup(int index, AlbumsIcon albumIcon) {
		if (indexOfPopup != -1) {
			((AlbumsIcon) (albumsTable.getWidget(indexOfPopup / 6, indexOfPopup % 6))).setPopupUnvisible();
		}
		albumIcon.setPopupVisible();
		indexOfPopup = index;
	}

}
